//
//  CZMenuViewController.m
//  i0S007通用项目
//
//  Created by Apple on 16/2/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "CZMenuViewController.h"
#import "CZTabBarController.h"
#import "CZMenuItem.h"
#import "CZMenuItemViewModel.h"
#import "CZMenuButton.h"
#import "CZDetailViewController.h"

const int ComposeStackViewLadnscapeHeight = 90;
const int ComposeStackViewPortraitHeight = 180;

const int UserIconMarginTop = 60;
const int UserIconLandscapeWH = 100;
const int UserIconPortraitWH = 44;

// MVVM : ViewMode把之前放在控制器或者模型里面的关于模型数据的处理放到ViewMode
@interface CZMenuViewController ()

// 容器视图,显示的是iPhone的界面,只有当splitViewController的宽度是紧凑的时候才需要显示出来
@property (weak, nonatomic) UIView *containerView;

@property (strong, nonatomic) UIStackView *composeStackView;

@property (nonatomic, strong) UIStackView *menuStackView;

@property (nonatomic, strong) CZMenuItemViewModel *menuItemVM;

/// 当前选中的按钮
@property (nonatomic, strong) CZMenuButton *selectedButton;

/// 用户头像
@property (nonatomic, strong) UIButton *userIconButton;

/// 用户名称
@property (nonatomic, strong) UILabel *userNameLabel;

@end

@implementation CZMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CommonBGColor;
    
    // 当显示iPhone界面时,需要把iPad相关的元素给隐藏起来
    [self prepareComposeStackView];
    [self prepareMenuStackView];
    [self prepareUserIcon];
    [self prepareUserNameLabel];
    
    // 将iPhoneJ界面盖在iPad相关元素的最上面
    [self setupPhoneContainerView];
    
    // 默认选中 全部动态
    [self menuButtonClick:self.menuStackView.arrangedSubviews[0]];
}

// 设置用户名称Label
- (void)prepareUserNameLabel {
    // 添加控件
    [self.view addSubview:self.userNameLabel];
    
    // 添加约束
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        // label的centerX和用户头像的centerX重合,当头像移动,label可以不用在修改约束
        make.centerX.equalTo(self.userIconButton);
        
        // label的顶部和按钮的底部重合,并且有16间距
        make.top.equalTo(self.userIconButton.mas_bottom).offset(16);
    }];
}

// 设置用户头像
- (void)prepareUserIcon {
    // 添加控件
    [self.view addSubview:self.userIconButton];
    
    // 设置约束
    [self.userIconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        
        // equalTo必须要传对象
        // mas_equalTo可以传基本数据类型和结构体
        // offest可以传基本数据类型
        make.top.equalTo(self.view).offset(UserIconMarginTop);
        make.size.mas_equalTo(CGSizeMake(UserIconLandscapeWH, UserIconLandscapeWH));
    }];
}

/**
 *  当设备屏幕发生改变的时候,来根据屏幕的方向设置子控件的布局
 *
 *  @param isLandscape 是否是横屏
 */
- (void)updateSubView:(BOOL)isLandscape {
    CGFloat height = isLandscape ? ComposeStackViewLadnscapeHeight : ComposeStackViewPortraitHeight;
    // 更新composeStackView的高度约束
    [self.composeStackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
    
    self.composeStackView.axis = isLandscape ? UILayoutConstraintAxisHorizontal : UILayoutConstraintAxisVertical;
    
    // 根据屏幕方向设置按钮的相关属性
    for (CZMenuButton *button in self.menuStackView.arrangedSubviews) {
        [button setupButton:isLandscape];
    }
    
    // 重新约束用户头像的大小
    [self.userIconButton mas_updateConstraints:^(MASConstraintMaker *make) {
        CGSize size = isLandscape ? CGSizeMake(UserIconLandscapeWH, UserIconLandscapeWH) : CGSizeMake(UserIconPortraitWH, UserIconPortraitWH);
        make.size.mas_equalTo(size);
    }];
    
    // 竖屏时隐藏用户名称
    self.userNameLabel.hidden = !isLandscape;
}

/**
 *  准备撰写相关的stackView
 */
- (void)prepareComposeStackView {
    // 1.将composeStackView添加为self.view的子view
    [self.view addSubview:self.composeStackView];
    
    // 设置UIStackView的相关属性
//    self.composeStackView.axis    // 默认是水平方向
//    self.composeStackView.alignment // 默认是Fill
    self.composeStackView.distribution = UIStackViewDistributionFillEqually;
    
    // 2.添加约束
    [self.composeStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.bottom.equalTo(self.view);
        
        make.height.equalTo(@(90));
    }];

    // UIStackView并不会渲染到屏幕上面,设置背景颜色等显示效果是没有用的

    // 4.创建按钮
    for (CZMenuItem *item in self.menuItemVM.compseItems) {
        CZMenuButton *button = [CZMenuButton menuButtonWithMenuItem:item];
        [button addTarget:self action:@selector(composeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        // 5.添加到UIStackView里面进行水平布局
        [self.composeStackView addArrangedSubview:button];
    }
}

- (void)prepareMenuStackView {
    // 1.将 menuStackView 添加为self.view的子view
    [self.view addSubview:self.menuStackView];
    
    // 2.设置UIStackView的相关属性
    self.menuStackView.axis = UILayoutConstraintAxisVertical;
    self.menuStackView.distribution = UIStackViewDistributionFillEqually;
    
    // 3.添加约束
    [self.menuStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.composeStackView.mas_top);
    }];
    
    // 5.创建按钮
    for (CZMenuItem *item in self.menuItemVM.menuItems) {
        CZMenuButton *button = [CZMenuButton menuButtonWithMenuItem:item];
        
        [button addTarget:self action:@selector(menuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(60));
        }];
        
        // 将按钮添加到menuStackView里面进行垂直平铺
        [self.menuStackView addArrangedSubview:button];
    }
}

#pragma mark 按钮点击事件
/// 设置点击的按钮变成选中状态
- (void)menuButtonClick:(CZMenuButton *)button {
    // 0. 如果当前点击的按钮已经是选中的按钮,就不需要往下面来执行
    if (self.selectedButton == button) {
        return;
    }
    
    // 1. 取消当前选中按钮的选中状态
    self.selectedButton.selected = NO;

    // 2. 让点击的按钮成为选中状态
    button.selected = YES;
    
    // 3. 将点击的按钮设置为选中的按钮
    self.selectedButton = button;

    // 4. 调用详情控制器的方法设置不同的内容控制器
    // 4.1拿到详情控制器
    CZDetailViewController *detailVC = self.splitViewController.childViewControllers[1];
    [detailVC setupContentViewWithMenuItem:button.menuItem];
}

- (void)composeButtonClick:(CZMenuButton *)button {
    // 1.获取模型里面对应控制器的名称
    Class cls = NSClassFromString(button.menuItem.detailContentVCName);
    
    // 2.创建控制器
    UIViewController *vc = [[cls alloc] init];
    vc.title = button.menuItem.title;
    
    // 3.包装导航栏
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    
    // 4.弹出控制器
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)setupPhoneContainerView {
    // 1.创建tabBar控制器
    CZTabBarController *tabBarVC = [[CZTabBarController alloc] init];
    
    // 2.让tabBarVC.view添加到self.view里面
    // 控制器的view是通过懒加载的形式创建出来的,
    [self.view addSubview:tabBarVC.view];
    
    // 如果一个控制器的view成为另外一个控制器的子view时,最好将控制器也成为另一个控制器的子控制器
    // 3.将tabBarVC作为self的子控制器,当屏幕旋转或modal的时候才不会出现问题
    [self addChildViewController:tabBarVC];
    
    // 4.将tabBarVC.view赋值给containerView.
    self.containerView = tabBarVC.view;
}

/**
 *  显示或隐藏容器视图
 *
 *  @param isShow YES:显示, NO:隐藏
 */
- (void)showContainerView:(BOOL)isShow {
    CZLog(@"showContainerView:%@", self.containerView);
    self.containerView.hidden = !isShow;
    
    // 显示iPhone界面,状态栏为黑色的
    [UIApplication sharedApplication].statusBarStyle = isShow ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent;
}

#pragma mark - 懒加载
- (UIStackView *)composeStackView {
    if (_composeStackView == nil) {
        _composeStackView = [[UIStackView alloc] init];
    }
    
    return _composeStackView;
}

- (UIStackView *)menuStackView {
    if (_menuStackView == nil) {
        _menuStackView = [[UIStackView alloc] init];
    }
    
    return _menuStackView;
}

-  (CZMenuItemViewModel *)menuItemVM {
    if (_menuItemVM == nil) {
        _menuItemVM = [[CZMenuItemViewModel alloc] init];
    }
    
    return _menuItemVM;
}

- (UIButton *)userIconButton {
    if (_userIconButton == nil) {
        _userIconButton = [[UIButton alloc] init];
        
        // 设置默认的头像
        [_userIconButton setImage:[UIImage imageNamed:@"default_person_lit"] forState:UIControlStateNormal];
    }
    
    return _userIconButton;
}

- (UILabel *)userNameLabel {
    if (_userNameLabel == nil) {
        _userNameLabel = [[UILabel alloc] init];
        
        _userNameLabel.text = @"iOS07通用项目";
        
        // 设置label的文字颜色
        _userNameLabel.textColor = [UIColor whiteColor];
    }
    
    return _userNameLabel;
}
@end